package com.sso.ssocasclient.util;

import org.jasig.cas.client.authentication.AttributePrincipal;
import org.jasig.cas.client.util.AbstractCasFilter;
import org.jasig.cas.client.validation.Assertion;

import javax.servlet.http.HttpServletRequest;

/**
 * 类 {@code CasUtil}  <br> 获取登录后的用户信息工具类.
 *
 * <p>详细描述
 * <p>
 * @author <a href="mailto:lz2392504@gmail.com">CN華少</a>
 * @since v1.0.0
 */
public class CasUtil {
    /**
     * 从cas中获取用户名
     *
     * @param request
     * @return
     */
    public static String getAccountNameFromCas(HttpServletRequest request) {
        Assertion assertion = (Assertion) request.getSession().getAttribute(AbstractCasFilter.CONST_CAS_ASSERTION);
        if(assertion!= null){
            AttributePrincipal principal = assertion.getPrincipal();
            return principal.getName();
        }else{
            return null;
        }
    }
}
